package LeetCode.LeetCode2;

/**
 * 两两交换链表中的结点
 * @author lumin
 * @date 2022/03/23 13:53
 **/
public class Num24 {
    public class ListNode {
       int val;
       ListNode next;
       ListNode() {}
       ListNode(int val) { this.val = val; }
       ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode swapPairs(ListNode head) {
        if(head == null && head.next == null){
            return head;
        }
        ListNode dummyHead = new ListNode(0,head);
        ListNode prev = dummyHead;
        while(prev.next != null && prev.next.next != null){
            ListNode node1 = prev.next;
            ListNode node2 = prev.next.next;
            prev.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            prev = node1;
        }
        return dummyHead.next;
    }
}